package com.maibow.service.app;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import sun.misc.BASE64Decoder;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.service.app
 * 文件名：BaseRequestApi.java
 * 作者: 吴比(Wubi)
 * 时间：2015年5月23日-下午5:32:16
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
public class BaseRequestApi {
    public static String requestApi(String order_type, String order_creator_id, String order_amount, String org_id, String seller_id, String reason, String url) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
        NameValuePair[] data = { new NameValuePair("order_type", order_type), new NameValuePair("order_creator_id", order_creator_id), new NameValuePair("order_amount", order_amount),
                new NameValuePair("org_id", org_id), new NameValuePair("seller_id", seller_id), new NameValuePair("reason", reason) };
        method.setRequestBody(data);
        String submitResult = null;
        try {
            int statusCode = client.executeMethod(method);
            if (statusCode == 200) {
                submitResult = method.getResponseBodyAsString();
            }
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = getBASE64Decode(submitResult);
        return result;
    }

    public static String getBASE64Decode(String str) {
        String ret = null;
        try {
            ret = new String(new BASE64Decoder().decodeBuffer(str), "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ret;
    }
}
